﻿@namespace ThingsGateway.RulesEngine

@using ThingsGateway.Blazor.Diagrams.Components.Renderers
@using ThingsGateway.Extension.Generic
@using ThingsGateway.Gateway.Application
@using ThingsGateway.NewLife.Extension
<div class="custom-node @(Node.Selected ? " selected" : "")">
    <div class="card" style="width: 250px;">
        <div class="card-body">
            <h6 class="card-title mb-2">@Localizer[Node.Title]</h6>
            <div class="form-group">
                <Select class="form-control"
                        @bind-Value="@Node.Text"
                        IsVirtualize
                        OnQueryAsync="(a)=>OnRedundantDevicesQuery(a)"
                        ShowSearch="true"
                        PlaceHolder="@Localizer[Node.Placeholder]"
                @onpointerdown:stopPropagation
                @onpointerup:stopPropagation
                @onpointermove:stopPropagation />
            </div>
        </div>
    </div>

    @foreach (var port in Node.Ports)
    {
        <PortRenderer @key="port" Port="port"></PortRenderer>
    }
</div>

@code {
    [Inject]
    IStringLocalizer<ThingsGateway.RulesEngine._Imports> Localizer { get; set; }

    [Parameter]
    public TextNode Node { get; set; }

    private static async Task<QueryData<SelectedItem>> OnRedundantDevicesQuery(VirtualizeQueryOption option)
    {
        var ret = new QueryData<SelectedItem>()
            {
                IsSorted = false,
                IsFiltered = false,
                IsAdvanceSearch = false,
                IsSearch = !option.SearchText.IsNullOrWhiteSpace()
            };

        var devices = await GlobalData.GetCurrentUserDevices().ConfigureAwait(false);
        var items = devices.WhereIf(!option.SearchText.IsNullOrWhiteSpace(), a => a.Name.Contains(option.SearchText)).Take(20)
           .Select(a => new SelectedItem(a.Name, a.Name));

        ret.TotalCount = items.Count();
        ret.Items = items;
        return ret;
    }


}
